#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2024/2/20 9:04
# @Author   : Nabby
# @FileName : upload_manage.py
# @ Desc    :  上传文件
import os.path
import uuid
from werkzeug.utils import secure_filename

from config import basedir

from infrastructure.utils.common import oss_tool
from infrastructure.utils.http import responser


# 上传文件
def upload_file(request):
    file = request.files["file"] if "file" in request.files else None
    data = {}
    if file:

        ext = secure_filename(file.filename).split(".")[-1]
        file_first_name = str(uuid.uuid4())
        file_name = f"{file_first_name}.{ext}"
        file_path = f'https://{oss_tool.bucket_name}.{oss_tool.Endpoint}/{file_name}'
        upload_result = oss_tool.oss_upload_file(file_name, file)
        if upload_result is False:
            return responser.send(46006)
        else:
            data["file_path"] = file_path
            return responser.send(10000, data=data)
    return responser.send(10000, data=data)